import logging
import os
from datetime import datetime

class LogManager:
    """日志管理器"""
    
    def __init__(self, log_dir='logs'):
        self.log_dir = log_dir
        self._setup_logging()
        
    def _setup_logging(self):
        """设置日志"""
        # 创建日志目录
        if not os.path.exists(self.log_dir):
            os.makedirs(self.log_dir)
            
        # 设置日志文件名
        log_file = os.path.join(
            self.log_dir,
            f"serial_disk_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
        )
        
        # 配置日志格式
        formatter = logging.Formatter(
            '%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S'
        )
        
        # 文件处理器
        file_handler = logging.FileHandler(log_file, encoding='utf-8')
        file_handler.setFormatter(formatter)
        
        # 控制台处理器
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(formatter)
        
        # 配置根日志记录器
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)
        logger.addHandler(file_handler)
        logger.addHandler(console_handler)
        
    @staticmethod
    def debug(msg, *args, **kwargs):
        logging.debug(msg, *args, stacklevel=2, **kwargs)
        
    @staticmethod
    def info(msg, *args, **kwargs):
        logging.info(msg, *args, stacklevel=2, **kwargs)
        
    @staticmethod
    def warning(msg, *args, **kwargs):
        logging.warning(msg, *args, stacklevel=2, **kwargs)
        
    @staticmethod
    def error(msg, *args, **kwargs):
        logging.error(msg, *args, stacklevel=2, **kwargs)
        
    @staticmethod
    def exception(msg, *args, **kwargs):
        logging.exception(msg, *args, stacklevel=2, **kwargs)
        
    @classmethod
    def init(cls):
        """初始化日志管理器"""
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S'
        ) 